import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

@Slf4j
//负责生产各式各样的消息到rabbitMQ
public class MessageSender {
    static Connection conn;
    @BeforeAll
    public static void createConn() throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.56.77");
        factory.setPort(5672);
        factory.setUsername("rabbitmq");
        factory.setPassword("rabbitmq");
        factory.setVirtualHost("/mdmall");
        conn = factory.newConnection();
    }
    @Test
    public void s0(){
        log.info("建立rabbitmq连接：{}",conn);
    }
    @Test
    public void s1() throws IOException, TimeoutException {
        Channel channel = conn.createChannel();
        String queueName = "simple_queue";
        /**
         * 声明一个队列
         * queueDeclare参数说明     队列名称   是否持久化   是否独占当前连接     消费完毕后是否自动删除   其他参数
         */
        channel.queueDeclare(queueName,true,false,true,null);
        String message = "你好rabbit   1234";
        /**
         * 发送一条消息
         * 参数  ：交换机    路由键       其他额外的属性    待发送的消息内容，字节数组，本例中选择utf、-8
         */
        channel.basicPublish("",queueName,null,message.getBytes(StandardCharsets.UTF_8));
        log.info("消息成功投递  {}",message);
        channel.close();
        conn.close();
    }
    @Test  //工作模式
    public void s2() throws IOException, InterruptedException, TimeoutException {
        Channel channel = conn.createChannel();//建立信道
        String queueName = "work_queue";
        channel.queueDeclare(queueName,true,false,true,null);
        //生产30条消息
        for (int i = 1;i<=30;i++){
            String message = "你好尼克"+i;
            channel.basicPublish("",queueName,null,message.getBytes(StandardCharsets.UTF_8));
            log.info("已成功发送第{}条消息，内容：  {}",i,message);
            TimeUnit.MILLISECONDS.sleep(500);
        }
        channel.close();
        conn.close();
    }
    @Test  //交换机模式
    public void s3() throws IOException, InterruptedException, TimeoutException{
        Channel channel = conn.createChannel();//建立信道
        String exchange = "fanout_exchange";
        String queue1 = "fanout_queue1",queue2 = "fanout_queue2";
        /**
         * 声明交换机
         * 参数：交换机名称    交换机类型（本例是扇形交换机）  是否进行持久化      是否自动删除消息     其他额外参数
         */
        channel.exchangeDeclare(exchange, BuiltinExchangeType.FANOUT,true,false,null);
        //声明两个队列
        channel.queueDeclare(queue1,true,false,false,null);
        channel.queueDeclare(queue2,true,false,false,null);
        /**
         * 绑定交换机和队列
         * 参数： 队列名称    交换机   交换机和队列绑定的路由键（空白为每一个队列）   其他参数
         */
        channel.queueBind(queue1,exchange,"",null);
        channel.queueBind(queue2,exchange,"",null);
        String message = "你好sadasdlk见是否变身卡";
        channel.basicPublish(exchange,"",null,message.getBytes(StandardCharsets.UTF_8));
        channel.close();
        conn.close();
    }
    @Test  //路由模式
    public void s4() throws IOException, InterruptedException, TimeoutException{
        Channel channel = conn.createChannel();//建立信道
        String exchange = "direct_exchange";//直连交换机
        String queue1 = "direct_queue1",queue2 = "direct_queue2";
        channel.exchangeDeclare(exchange,BuiltinExchangeType.DIRECT,true,false,null);
        //声明两个队列
        channel.queueDeclare(queue1,true,false,false,null);
        channel.queueDeclare(queue2,true,false,false,null);
        //绑定交换机和队列
        channel.queueBind(queue1,exchange,"sport",null);
        channel.queueBind(queue2,exchange,"traffic",null);
        channel.queueBind(queue2,exchange,"study",null);
        channel.basicPublish(exchange,"sport",null,"两只老虎跑得快sport".getBytes(StandardCharsets.UTF_8));
        channel.basicPublish(exchange,"traffic",null,"罚款剑法时间基本撒旦traffic".getBytes(StandardCharsets.UTF_8));
        channel.basicPublish(exchange,"study",null,"喀什给发吉萨高地开发商study".getBytes(StandardCharsets.UTF_8));
        log.info("投递完成");
        channel.close();
        conn.close();
    }
    /**
     * 诗词的绑定键  朝代，作者，主题
     * 唐·李白 友情
     * 例：  *.*.友情    #.友情（前边都不限）        *.李白.*
     */
    @Test  //主题模式
    public void s5() throws IOException, InterruptedException, TimeoutException{
        Channel channel = conn.createChannel();//建立信道
        String exchange = "topic_exchange";//主题交换机
        channel.exchangeDeclare(exchange,BuiltinExchangeType.TOPIC,true,false,null);
        String queue1 = "topic_queue1",queue2 = "topic_queue2";
        //声明两个队列
        channel.queueDeclare(queue1,true,false,false,null);
        channel.queueDeclare(queue2,true,false,false,null);
        //绑定交换机和队列
        channel.queueBind(queue1,exchange,"*.*.friend");
        channel.queueBind(queue2,exchange,"*.libai.*");
        channel.queueBind(queue2,exchange,"#.view");
        String m1 = "日照香炉生紫烟" ,m2 = "桃花潭水深千尺";
        String m3 = "会当凌绝顶" ,m4 = "天下谁人不识君";
        channel.basicPublish(exchange,"tang.libai.view",null,m1.getBytes(StandardCharsets.UTF_8));
        channel.basicPublish(exchange,"tang.libai.friend",null,m2.getBytes(StandardCharsets.UTF_8));
        channel.basicPublish(exchange,"tang.dufu.view",null,m3.getBytes(StandardCharsets.UTF_8));
        channel.basicPublish(exchange,"tang.gaoshi.friend",null,m4.getBytes(StandardCharsets.UTF_8));
        log.info("投递完成");
        channel.close();
        conn.close();
    }
}
